#include "stdafx.h"
#include "Complex.h"

void copy_complex(Complex* complex, unsigned int length, std::vector<Complex>& destination)
{
	if(destination.size()!=length)
	{
		destination.resize(length);
	}
	for(unsigned int i=0; i<length; i++)
	{
		destination[i] = complex[i];		
	}
}

void copy_complex(std::vector<Complex>& origin, std::vector<Complex>& destination)
{
	copy_complex(&origin[0], origin.size(), destination);
}

void short_to_complex(short* src, Complex* dest, unsigned int length)
{
	for(register unsigned int i=0; i<length; i++)
	{
		dest[i].r = src[i] / (double)SHORT_MAX;
		dest[i].i = 0;
	}
}

void complex_to_short(Complex* src, short* dest, unsigned int length)
{
	for(register unsigned int i=0; i<length; i++)
    {
      double sample = src[i].r;
      if (sample < -1.0) 
		  sample = -1.0;
      else if (sample > 1.0) 
		  sample = 1.0;
      dest[i] = (short)(sample * SHORT_MAX);
    }
}


/*
Multiplies two complex arrays, and puts the results in the first array
*/
void fft_convolution(Complex* complex1, Complex* complex2, unsigned int length)
{
	for(unsigned int i=0; i<length; i++)
	{
		double temp = complex1[i].r*complex2[i].r - complex1[i].i*complex2[i].i;
		complex1[i].i = complex1[i].r*complex2[i].i + complex1[i].i*complex2[i].r;
		complex1[i].r = temp;
	}
}
